package com.star.utils;


import lombok.Data;
import org.joda.time.DateTime;

import java.sql.Timestamp;
import java.util.Date;

/**
 * @author chaoyang
 * 统一返回Rest风格的数据结构
 */
@Data
public class RestResult {
    /**
     * 请求是否成功
     */
    private boolean success;
    /**
     * 成功或者失败的code错误码
     */
    private String code;
    /**
     * 成功时返回的数据，失败时返回具体的异常信息
     */
    private Object data;
    /**
     * 请求失败返回的提示信息，给前端进行页面展示的信息
     */
    private Object message;
    /**
     * 服务器当前时间（添加该字段的原因是便于查找定位请求时间，因为实际开发过程中服务器时间可能跟本地时间不一致，加上这个时间戳便于日后定位）
     */
    private Timestamp currentTime = new Timestamp(new Date().getTime());

    public RestResult() {
    }

    @Override
    public String toString() {
        return "RestResult{" +
                "success=" + success +
                ", code='" + code + '\'' +
                ", data=" + data +
                ", message=" + message +
                ", currentTime=" + currentTime +
                '}';
    }

    public RestResult(boolean success, String code, Object data, Object message) {
        this.success = success;
        this.code = code;
        this.data = data;
        this.message = message;
        this.currentTime = new Timestamp(new DateTime().getMillis());
    }

/*    abstract class Builder<T>{

        abstract T code();

        abstract T message();

        abstract  T data();

        abstract T success();
    }*/

    public static class RestResultBuilder {

        private RestResult restResult = new RestResult();

        public RestResult builder() {

            return restResult;
        }

        public RestResultBuilder success(boolean success) {
            restResult.setSuccess(success);

            return this;
        }

        public RestResultBuilder data(Object data) {
            restResult.setData(data);
            return this;
        }

        public RestResultBuilder message(String message) {
            restResult.setMessage(message);
            return this;
        }

        public RestResultBuilder code(String code) {
            restResult.setCode(code);
            return this;
        }


    }


}
